package com.youku.util;

import java.util.List;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class HtmlMailHelper {

	/**
	 * 多次发送无法使用同一个邮件HtmlEmail对象。否则会导致邮件服务器内容重复，多次发送了同样的邮件信息
	 * @param subject
	 * @param html
	 * @param emails
	 */
	public static void send(String subject, String html, List<String> emails) {
		try {
			HtmlEmail email = initEmailClient();
			email.addTo(emails.toArray(new String[0]));
			email.setSubject(subject);
			email.setHtmlMsg(html);
			email.setCharset(Consts.DEFAULT_ENCODING);
			email.setTextMsg("你的邮件客户端不支持html格式邮件");
			email.send();
		} catch (EmailException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static HtmlEmail initEmailClient() throws EmailException{
		HtmlEmail email = new HtmlEmail();
		email.setHostName("smtp.sina.com.cn");
		email.setAuthentication("mjaow@sina.com", "83949177");
		email.setFrom("mjaow@sina.com", "mjaow");
		return email;
	}

}
